package com.xydtech.ecard.ecard.service.impl;

import com.xydtech.common.core.text.Convert;
import com.xydtech.common.utils.AESEncryptionUtils;
import com.xydtech.common.utils.DateUtils;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.ecard.ecard.domain.EcardClientInfo;
import com.xydtech.ecard.ecard.mapper.EcardClientInfoMapper;
import com.xydtech.ecard.ecard.service.IEcardClientInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 个人客户基本信息Service业务层处理
 *
 * @author xydtech
 * @date 2024-05-21
 */
@Service
public class EcardClientInfoServiceImpl implements IEcardClientInfoService {
    @Autowired
    private EcardClientInfoMapper ecardClientInfoMapper;

    /**
     * 查询个人客户基本信息
     *
     * @param id 个人客户基本信息主键
     * @return 个人客户基本信息
     */
    @Override
    public EcardClientInfo selectEcardClientInfoById(String id, String salt) {
        EcardClientInfo ecardClientInfo = ecardClientInfoMapper.selectEcardClientInfoById(id);
        if (ecardClientInfo != null) {
            try {
                //数据解密（手机号、身份证号）
                if (!StringUtils.objIsNullOrEmpty(ecardClientInfo.getPhoneNum())) {
                    ecardClientInfo.setPhoneNum(AESEncryptionUtils.decrypt(ecardClientInfo.getPhoneNum(), salt));
                }
                if (!StringUtils.objIsNullOrEmpty(ecardClientInfo.getCardNum())) {
                    ecardClientInfo.setCardNum(AESEncryptionUtils.decrypt(ecardClientInfo.getCardNum(), salt));
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return ecardClientInfo;
    }

    @Override
    public EcardClientInfo getEcardClientInfoByCardNum(String cardNum, String salt) {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(cardNum)) {
                cardNum = (AESEncryptionUtils.encrypt(cardNum, salt));
            }
            EcardClientInfo clientInfo = ecardClientInfoMapper.getEcardClientInfoByCardNum(cardNum);
            if (clientInfo != null) {
                //数据解密（手机号、身份证号）
                if (!StringUtils.objIsNullOrEmpty(clientInfo.getPhoneNum())) {
                    clientInfo.setPhoneNum(AESEncryptionUtils.decrypt(clientInfo.getPhoneNum(), salt));
                }
                if (!StringUtils.objIsNullOrEmpty(clientInfo.getCardNum())) {
                    clientInfo.setCardNum(AESEncryptionUtils.decrypt(clientInfo.getCardNum(), salt));
                }
            }
            return clientInfo;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 查询个人客户基本信息列表
     *
     * @param ecardClientInfo 个人客户基本信息
     * @return 个人客户基本信息
     */
    @Override
    public List<EcardClientInfo> selectEcardClientInfoList(EcardClientInfo ecardClientInfo, String salt) {
        List<EcardClientInfo> ecardClientInfoList = ecardClientInfoMapper.selectEcardClientInfoList(ecardClientInfo);
        if (ecardClientInfoList.size() > 0) {
            for (EcardClientInfo clientInfo : ecardClientInfoList) {
                try {
                    //数据解密（手机号、身份证号）
                    if (!StringUtils.objIsNullOrEmpty(clientInfo.getPhoneNum())) {
                        clientInfo.setPhoneNum(AESEncryptionUtils.decrypt(clientInfo.getPhoneNum(), salt));
                    }
                    if (!StringUtils.objIsNullOrEmpty(clientInfo.getCardNum())) {
                        clientInfo.setCardNum(AESEncryptionUtils.decrypt(clientInfo.getCardNum(), salt));
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return ecardClientInfoList;
    }

    /**
     * 新增个人客户基本信息
     *
     * @param ecardClientInfo 个人客户基本信息
     * @return 结果
     */
    @Override
    public int insertEcardClientInfo(EcardClientInfo ecardClientInfo, String salt) {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardClientInfo.getPhoneNum())) {
                ecardClientInfo.setPhoneNum(AESEncryptionUtils.encrypt(ecardClientInfo.getPhoneNum(), salt));
            }
            if (!StringUtils.objIsNullOrEmpty(ecardClientInfo.getCardNum())) {
                ecardClientInfo.setCardNum(AESEncryptionUtils.encrypt(ecardClientInfo.getCardNum(), salt));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        ecardClientInfo.setCreateTime(DateUtils.getNowDate());
        return ecardClientInfoMapper.insertEcardClientInfo(ecardClientInfo);
    }

    /**
     * 修改个人客户基本信息
     *
     * @param ecardClientInfo 个人客户基本信息
     * @return 结果
     */
    @Override
    public int updateEcardClientInfo(EcardClientInfo ecardClientInfo, String salt) {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardClientInfo.getPhoneNum())) {
                ecardClientInfo.setPhoneNum(AESEncryptionUtils.encrypt(ecardClientInfo.getPhoneNum(), salt));
            }
            if (!StringUtils.objIsNullOrEmpty(ecardClientInfo.getCardNum())) {
                ecardClientInfo.setCardNum(AESEncryptionUtils.encrypt(ecardClientInfo.getCardNum(), salt));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ecardClientInfoMapper.updateEcardClientInfo(ecardClientInfo);
    }

    /**
     * 批量删除个人客户基本信息
     *
     * @param ids 需要删除的个人客户基本信息主键
     * @return 结果
     */
    @Override
    public int deleteEcardClientInfoByIds(String ids) {
        return ecardClientInfoMapper.deleteEcardClientInfoByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除个人客户基本信息信息
     *
     * @param id 个人客户基本信息主键
     * @return 结果
     */
    @Override
    public int deleteEcardClientInfoById(String id) {
        return ecardClientInfoMapper.deleteEcardClientInfoById(id);
    }
}
